<template>  
	<div class="pagination">
    <button @click="changePageNo(pageNo-1)" :disabled="pageNo === 1">上一页</button>
    <button  @click="changePageNo(1)" v-show="startend.start !== 1">1</button>
    <span  v-show="startend.start > 2">···</span>
    
    <!-- <button>{{ startend.start }}</button>
    <button>7</button>
    <button class="active">{{ pageNo }}</button>    
    <button>9</button>
    <button>{{ startend.end }}</button> -->

    <button  @click="changePageNo(startend.start + index)" :class="{active:pageNo === startend.start + index}" v-for="(n,index) in (startend.end - startend.start + 1)" :key="index">{{ startend.start + index }}</button>
    
    <span v-show="startend.end < pageTotal-1">···</span>
    <button @click="changePageNo(pageTotal)" v-show="startend.end !== pageTotal">{{ pageTotal }}</button>
    <button @click="changePageNo(pageNo + 1)" :disabled="pageNo === pageTotal">下一页</button>
    
    <span>共 {{ total }} 条</span>
  </div>
</template>

<script>    
  export default {      
    name: "Pagination",    
    props:{
        total:{
            required:true
        },
        pageSize:{
            type:Number,
            required:true
        },
        pageNo:{
            type:Number,
            required:true
        },
        continues:{
            type:Number,
            required:true
        },
        changePageNo:{
          type:Function,
          required:true
        }
    },
    computed:{
        pageTotal(){
            return Math.ceil(this.total/this.pageSize)
        },
        // 计算连续分页的页码
        startend(){
            let {pageNo,continues,totalPage,pageTotal} = this;
            let start = 0;
            let end = 0;
            // 判断连续页码是否大于所有页码
            if(continues > pageTotal){
              start = 1
              end = pageTotal
            }else{
              start = pageNo - (continues - 1)/2;
              end = pageNo + (continues - 1)/2
              if(start < 1){
                start = 1
                end = continues
              }
            //   console.log(pageTotal);
              if(end > pageTotal){
                start = pageTotal - continues + 1
                end = pageTotal
              }
            }
            
            return {
              start,
              end
            }
        } 
    }
  };
</script>

<style lang="less" scoped>
  .pagination {
    text-align: center;

    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: gray;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #c81623;
        color: #fff;
      }
    }

    span {
      display: inline-block;
      line-height: 28px;
      font-size: 14px;
      color: gray;
      vertical-align: middle;
    }
  }
</style>